import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../../theme/theme_provider.dart';

class SettingsController extends ChangeNotifier {
  // تەڭشەك قىممەتلىرى
  bool _isDarkMode = false;
  bool _isNotificationEnabled = true;
  bool _isAutoBackupEnabled = true;
  String _language = 'ئۇيغۇرچە';
  String _currency = 'CNY ¥';
  double _textSize = 14.0;
  
  // تېما كونتروللىرى
  final ThemeProvider? themeProvider;
  
  // دېتال مەلۇماتلىرى
  final String _version = '1.0.0';
  final String _buildNumber = '100';
  
  // SharedPreferences ئۈچۈن ئاچقۇچلار
  static const String _darkModeKey = 'dark_mode';
  static const String _notificationKey = 'notifications';
  static const String _autoBackupKey = 'auto_backup';
  static const String _languageKey = 'language';
  static const String _currencyKey = 'currency';
  static const String _textSizeKey = 'text_size';
  
  // كونتروللىر قۇرۇلغاندا تەڭشەكلەرنى ئوقۇش
  SettingsController({this.themeProvider}) {
    _loadSettings();
    
    // ئەگەر تېما كونتروللىرى بولسا، ئۇنىڭدىن قاراڭغۇ ھالەت قىممىتىنى ئالىمىز
    if (themeProvider != null) {
      _isDarkMode = themeProvider!.isDarkMode;
    }
  }
  
  // تەڭشەكلەرنى ئوقۇش
  Future<void> _loadSettings() async {
    final prefs = await SharedPreferences.getInstance();
    
    _isNotificationEnabled = prefs.getBool(_notificationKey) ?? true;
    _isAutoBackupEnabled = prefs.getBool(_autoBackupKey) ?? true;
    _language = prefs.getString(_languageKey) ?? 'ئۇيغۇرچە';
    _currency = prefs.getString(_currencyKey) ?? 'CNY ¥';
    _textSize = prefs.getDouble(_textSizeKey) ?? 14.0;
    
    // ئەگەر تېما كونتروللىرى بولمىسا، ئۇنداقتا SharedPreferences تىن ئوقۇيمىز
    if (themeProvider == null) {
      _isDarkMode = prefs.getBool(_darkModeKey) ?? false;
    }
    
    notifyListeners();
  }
  
  // تەڭشەكلەرنى ساقلاش
  Future<void> _saveSettings() async {
    final prefs = await SharedPreferences.getInstance();
    
    // ئەگەر تېما كونتروللىرى بولمىسا، ئۇنداقتا SharedPreferences گە ساقلايمىز
    if (themeProvider == null) {
      await prefs.setBool(_darkModeKey, _isDarkMode);
    }
    
    await prefs.setBool(_notificationKey, _isNotificationEnabled);
    await prefs.setBool(_autoBackupKey, _isAutoBackupEnabled);
    await prefs.setString(_languageKey, _language);
    await prefs.setString(_currencyKey, _currency);
    await prefs.setDouble(_textSizeKey, _textSize);
  }
  
  // تەڭشەكلەرنى دەسلەپكى ھالەتكە قايتۇرۇش
  Future<void> resetToDefaults() async {
    _isDarkMode = false;
    _isNotificationEnabled = true;
    _isAutoBackupEnabled = true;
    _language = 'ئۇيغۇرچە';
    _currency = 'CNY ¥';
    _textSize = 14.0;
    
    // ئەگەر تېما كونتروللىرى بولسا، ئۇنىڭغا يېڭى قىممەت بېرىمىز
    if (themeProvider != null) {
      await themeProvider!.setDarkMode(false);
    }
    
    await _saveSettings();
    notifyListeners();
  }
  
  // گېتتېرلار
  bool get isDarkMode => themeProvider?.isDarkMode ?? _isDarkMode;
  bool get isNotificationEnabled => _isNotificationEnabled;
  bool get isAutoBackupEnabled => _isAutoBackupEnabled;
  String get language => _language;
  String get currency => _currency;
  double get textSize => _textSize;
  String get version => _version;
  String get buildNumber => _buildNumber;
  
  // سېتتېرلار
  void toggleDarkMode() async {
    if (themeProvider != null) {
      await themeProvider!.toggleTheme();
    } else {
      _isDarkMode = !_isDarkMode;
      await _saveSettings();
    }
    notifyListeners();
  }
  
  void toggleNotifications() async {
    _isNotificationEnabled = !_isNotificationEnabled;
    await _saveSettings();
    notifyListeners();
  }
  
  void toggleAutoBackup() async {
    _isAutoBackupEnabled = !_isAutoBackupEnabled;
    await _saveSettings();
    notifyListeners();
  }
  
  void setLanguage(String language) async {
    _language = language;
    await _saveSettings();
    notifyListeners();
  }
  
  void setCurrency(String currency) async {
    _currency = currency;
    await _saveSettings();
    notifyListeners();
  }
  
  void setTextSize(double size) async {
    _textSize = size;
    await _saveSettings();
    notifyListeners();
  }
} 